package sdk.org.apache.shiro.G2_使用.C09_缓存_Realm缓存_无效;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.subject.Subject;

import com.titan.core.utils.debug.Timer;
import com.titan.core.utils.io.ConsoleUtils;

import sdk.org.apache.shiro.utils.ShiroUtils;

public class Realm缓存_单java实现类 {
    public static void main(String[] args) {
        Timer timer = Timer.$();
        try {
            run(timer);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            timer.print("执行总用时");
            System.exit(0);
        }
    }
    
    private static void run(Timer timer) throws Exception {
        EhCacheManager cacheManager = new EhCacheManager();
        cacheManager.setCacheManagerConfigFile("classpath:sdk/org/apache/shiro/G2_使用/C09_缓存_Realm缓存_无效/shiro-ehcache.xml");
        Cache<Object, AuthorizationInfo> authorizationCache = cacheManager.getCache("authorizationCache");
        Cache<Object, AuthenticationInfo> authenticationCache = cacheManager.getCache("authenticationCache");
        
        RetryLimitHashedCredentialsMatcher credentials = new RetryLimitHashedCredentialsMatcher();
        credentials.setHashAlgorithmName(Md5Hash.ALGORITHM_NAME);
        credentials.setHashIterations(2);
        credentials.setStoredCredentialsHexEncoded(false);
        
        UserRealm realm = new UserRealm();
        realm.putAccount("admin", "1234");
        realm.setCachingEnabled(true);
        realm.setAuthenticationCachingEnabled(true);    // 启用身份验证缓存
        realm.setAuthenticationCacheName("authenticationCache");
        realm.setAuthenticationCache(authenticationCache);
        realm.setAuthorizationCachingEnabled(true);     // 启用授权缓存
        realm.setAuthorizationCacheName("authorizationCache");
        realm.setAuthorizationCache(authorizationCache);
        realm.setCredentialsMatcher(credentials);
        
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        securityManager.setCacheManager(cacheManager);
        securityManager.setRealm(realm);
        SecurityUtils.setSecurityManager(securityManager);
        
        // 登录验证
        loginTest("admin", "1234");
        
        // 修改密码，但是没有更新缓存
        realm.putAccount("admin", "123456");
        loginTest("admin", "123456");
    }
    
    
    private static void loginTest(String username, String password) throws Exception {
        for (int i = 0; i < 1; i++) {
            Subject currentUser = ShiroUtils.login(username, password, true);
            try {
                ConsoleUtils.log("用户 ["+currentUser.getPrincipal()+"] 的登录状态："+currentUser.isAuthenticated());
            } finally {
                ShiroUtils.logout();
            }
        }
    }
}
